//import 'dart:convert';

import 'package:flutter/material.dart';
//import 'package:shared_preferences/shared_preferences.dart';
import 'package:wx_chat/common/storage.dart';

class LocalstoragePage extends StatefulWidget {
  LocalstoragePage({Key key}) : super(key: key);

  @override
  _LocalstoragePageState createState() => _LocalstoragePageState();
}

class _LocalstoragePageState extends State<LocalstoragePage> {


  Map userinfo;

  _saveData() async{
    // SharedPreferences prs = await SharedPreferences.getInstance();
    // String value = json.encode(userInfo);
    // prs.setString("userinfo", value);
    Map userInfo = {
      "usernem":"王五",
      "age":20,
      "sex":"男",
      "love":"编程"
    };
    bool issuccess =await Storage.setMap("userinfo",userInfo);
    print(issuccess);
  }

  _getData() async {
    // SharedPreferences prs = await SharedPreferences.getInstance();
    // prs.getString("userinfo");
    // print(prs.getString("userinfo"));
    // String value = prs.getString("userinfo");
    // Map mapvalue = json.decode(value);
    // print(mapvalue["usernem"]);
    Map value = await Storage.getMap("userinfo");
    setState(() {
      userinfo = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("本地存储"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text("保存本地数据"),
              onPressed: this._saveData,
            ),
            RaisedButton(
              child: Text("取本地数据"),
              onPressed: this._getData,
            ),
            Text( userinfo!= null ? userinfo["usernem"] : ""),
            Text(userinfo!= null ? userinfo["age"].toString() : ""),
            Text(userinfo!= null ? userinfo["sex"] : "")
          ],
        ),
      ),
    );
  }
}
